<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-row>
              <el-col :span="6">
                <el-form-item label="支付单号" prop="payNo">
                  <el-input v-model="queryParams.payNo" placeholder="请输入支付单号" clearable @keyup.enter="handleQuery" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="微信交易号" prop="tradeNo" label-width="100">
                  <el-input v-model="queryParams.tradeNo" placeholder="请输入微信交易号" clearable @keyup.enter="handleQuery" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="订单号" prop="body">
                  <el-input v-model="queryParams.body" placeholder="请输入订单号" clearable @keyup.enter="handleQuery" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="服务名称" prop="subject">
                  <el-input v-model="queryParams.subject" placeholder="请输入服务名称" clearable @keyup.enter="handleQuery" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="6">
                <el-form-item label="支付时间" prop="payTime" style="width: 308px">
                  <el-date-picker
                    v-model="dateRange"
                    value-format="YYYY-MM-DD"
                    type="daterange"
                    range-separator="-"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item>
                  <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                  <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
<!--          <el-col :span="1.5">-->
<!--            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['lawyer:payOrder:add']">新增</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['lawyer:payOrder:edit']">修改</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['lawyer:payOrder:remove']">删除</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['lawyer:payOrder:export']">导出</el-button>-->
<!--          </el-col>-->
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="payOrderList" @selection-change="handleSelectionChange">
<!--        <el-table-column type="selection" width="55" align="center" />-->
        <el-table-column label="主键ID" align="center" prop="id" v-if="false" />
<!--        <el-table-column label="订单编号" align="center" prop="payNo" />-->
<!--        <el-table-column label="支付应用ID" align="center" prop="appId" />-->
<!--        <el-table-column label="微信appid" align="center" prop="wxAppId" />-->
<!--        <el-table-column label="商户号" align="center" prop="mchNo" />-->
<!--        <el-table-column label="商户名称" align="center" prop="mchName" />-->
<!--        <el-table-column label="通道ID" align="center" prop="channelId" />-->
<!--        <el-table-column label="通道编码" align="center" prop="channelCode" />-->
<!--        <el-table-column label="用户ID" align="center" prop="userId" />-->
        <el-table-column label="openId" align="center" prop="openId" />
        <el-table-column label="支付单号" align="center" prop="payNo" />
        <el-table-column label="微信交易号" align="center" prop="tradeNo" />
<!--        <el-table-column label="订单ID" align="center" prop="orderId" />-->
        <el-table-column label="服务名称" align="center" prop="subject" />
        <el-table-column label="服务描述" align="center" prop="body" />
<!--        <el-table-column label="付款方式" align="center" prop="payWay" />-->
        <el-table-column label="支付状态" align="center" prop="payStatus">
          <template #default="scope">
            <el-tag type="warning" v-if="scope.row.payStatus === 0">待支付</el-tag>
            <el-tag type="success" v-else-if="scope.row.payStatus === 1">支付成功</el-tag>
            <el-tag v-else-if="scope.row.payStatus === 2">已退款</el-tag>
            <el-tag type="info" v-else-if="scope.row.payStatus === 3">支付关闭</el-tag>
            <el-tag type="info" v-else>其他</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="支付时间" align="center" prop="payTime" width="180" />
        <el-table-column label="支付金额" align="center" prop="amount">
          <template #default="scope">
            <el-text>{{ scope.row.amount / 100 }} 元</el-text>
          </template>
        </el-table-column>
<!--        <el-table-column label="退款金额" align="center" prop="refundPrice" />-->
<!--        <el-table-column label="回调时间" align="center" prop="returnTime" width="180"/>-->
<!--        <el-table-column label="回调地址" align="center" prop="notifyUrl" />-->
<!--        <el-table-column label="手续费比率" align="center" prop="procedureKb" />-->
<!--        <el-table-column label="手续费" align="center" prop="procedureFee" />-->
<!--        <el-table-column label="订单退款成功时间" align="center" prop="successTime" width="180">-->
<!--          <template #default="scope">-->
<!--            <span>{{ parseTime(scope.row.successTime, '{y}-{m}-{d}') }}</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column label="扩展参数" align="center" prop="extParam" />-->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="详情" placement="top">
              <el-button link type="primary" icon="InfoFilled" @click="handleUpdate(scope.row)" v-hasPermi="['pay:order:query']"></el-button>
            </el-tooltip>
<!--            <el-tooltip content="删除" placement="top">-->
<!--              <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['lawyer:payOrder:remove']"></el-button>-->
<!--            </el-tooltip>-->
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改支付订单对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="839px" append-to-body>
      <el-form ref="payOrderFormRef" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="用户openId" prop="openId">
          <el-input v-model="form.openId" readonly/>
        </el-form-item>
        <el-form-item label="支付单号" prop="payNo">
          <el-input v-model="form.payNo" readonly />
        </el-form-item>
        <el-form-item label="微信交易号" prop="tradeNo">
          <el-input v-model="form.tradeNo" readonly />
        </el-form-item>
        <el-form-item label="服务名称" prop="subject">
          <el-input v-model="form.subject" readonly />
        </el-form-item>
        <el-form-item label="服务描述" prop="body">
          <el-input v-model="form.body" type="textarea" :rows="2" readonly />
        </el-form-item>
        <el-form-item label="支付金额" prop="amount">
          <el-text>{{ form.amount / 100 }} 元</el-text>
        </el-form-item>
        <el-form-item label="支付状态" prop="amount">
          <el-text v-if="form.payStatus === 0">待支付</el-text>
          <el-text v-else-if="form.payStatus === 1">支付成功</el-text>
          <el-text v-else-if="form.payStatus === 2">已退款</el-text>
          <el-text v-else-if="form.payStatus === 3">支付关闭</el-text>
          <el-text v-else>其他</el-text>
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
            <el-text>{{ form.payTime }} </el-text>
        </el-form-item>
<!--        <el-form-item label="步骤" prop="payStep">-->
<!--          <el-input v-model="form.payStep" placeholder="请输入步骤(1 创建支付，2 支付回调通知)" />-->
<!--        </el-form-item>-->

<!--        <el-form-item label="退款金额" prop="refundPrice">-->
<!--          <el-input v-model="form.refundPrice" placeholder="请输入退款金额" />-->
<!--        </el-form-item>-->
        <el-form-item label="错误码" prop="resultCode">
          <el-input v-model="form.resultCode"  readonly/>
        </el-form-item>
        <el-form-item label="错误信息" prop="resultMsg">
          <el-input v-model="form.resultMsg" type="textarea" :rows="3" readonly />
        </el-form-item>
        <el-form-item label="提交的信息" prop="postJson">
          <el-input v-model="form.postJson" type="textarea" :rows="6" readonly />
        </el-form-item>
        <el-form-item label="回调的信息" prop="returnJson">
          <el-input v-model="form.returnJson" type="textarea" :rows="6" readonly />
        </el-form-item>
        <el-form-item label="回调时间" prop="returnTime">
            <el-text>{{ form.returnTime }}</el-text>
        </el-form-item>
        <el-form-item label="回调地址" prop="notifyUrl">
          <el-input v-model="form.notifyUrl" type="textarea" :rows="3" readonly/>
        </el-form-item>
<!--        <el-form-item label="手续费比率" prop="procedureKb">-->
<!--          <el-input v-model="form.procedureKb" placeholder="请输入手续费比率" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="手续费" prop="procedureFee">-->
<!--          <el-input v-model="form.procedureFee" placeholder="请输入手续费" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="订单退款成功时间" prop="successTime">-->
<!--          <el-date-picker clearable-->
<!--                          v-model="form.successTime"-->
<!--                          type="datetime"-->
<!--                          value-format="YYYY-MM-DD HH:mm:ss"-->
<!--                          placeholder="请选择订单退款成功时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
        <el-form-item label="扩展参数" prop="extParam">
          <el-input v-model="form.extParam" type="textarea" :rows="3" readonly/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="cancel">关 闭</el-button>
<!--          <el-button @click="cancel">取 消</el-button>-->
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PayOrder" lang="ts">
import { listPayOrder, getPayOrder, delPayOrder, addPayOrder, updatePayOrder } from '@/api/pay/payorder';
import { PayOrderVo, PayOrderQuery, PayOrderForm } from '@/api/pay/payorder/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const payOrderList = ref<PayOrderVo[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);
const queryFormRef = ref<ElFormInstance>();
const payOrderFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: PayOrderForm = {
  id: undefined,
  appId: undefined,
  wxAppId: undefined,
  mchNo: undefined,
  mchName: undefined,
  channelId: undefined,
  channelCode: undefined,
  userId: undefined,
  openId: undefined,
  payNo: undefined,
  tradeNo: undefined,
  orderId: undefined,
  subject: undefined,
  body: undefined,
  payWay: undefined,
  payStatus: undefined,
  payTime: undefined,
  payStep: undefined,
  postStatus: undefined,
  returnStatus: undefined,
  amount: undefined,
  refundPrice: undefined,
  resultCode: undefined,
  resultMsg: undefined,
  postJson: undefined,
  returnJson: undefined,
  returnTime: undefined,
  notifyUrl: undefined,
  procedureKb: undefined,
  procedureFee: undefined,
  successTime: undefined,
  extParam: undefined,
}
const data = reactive<PageData<PayOrderForm, PayOrderQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    appId: undefined,
    wxAppId: undefined,
    mchNo: undefined,
    mchName: undefined,
    channelId: undefined,
    channelCode: undefined,
    userId: undefined,
    openId: undefined,
    payNo: undefined,
    tradeNo: undefined,
    orderId: undefined,
    subject: undefined,
    body: undefined,
    payWay: undefined,
    payStatus: undefined,
    payTime: undefined,
    payStep: undefined,
    postStatus: undefined,
    returnStatus: undefined,
    amount: undefined,
    refundPrice: undefined,
    resultCode: undefined,
    resultMsg: undefined,
    postJson: undefined,
    returnJson: undefined,
    returnTime: undefined,
    notifyUrl: undefined,
    procedureKb: undefined,
    procedureFee: undefined,
    successTime: undefined,
    extParam: undefined,
    params: {
    }
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询支付订单列表 */
const getList = async () => {
  loading.value = true;
  const res = await listPayOrder(proxy?.addDateRange(queryParams.value, dateRange.value));
  payOrderList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  payOrderFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = ['', ''];
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: PayOrderVo[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加支付订单';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: PayOrderVo) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getPayOrder(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改支付订单';
}

/** 提交按钮 */
const submitForm = () => {
  payOrderFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updatePayOrder(form.value).finally(() => buttonLoading.value = false);
      } else {
        await addPayOrder(form.value).finally(() => buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: PayOrderVo) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除当前选中的数据项？').finally(() => loading.value = false);
  await delPayOrder(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('lawyer/payOrder/export', {
    ...queryParams.value
  }, `PayOrder_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
